package com.bcgtgjyb.huanwen.customview.mylibrary;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by bigwen on 2016/1/14.
 */
public class LoadingLine extends Component implements Component.DrawTask {
    EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner());
    private Paint paint;
    private Paint backPaint;
    private Paint textPaint;
    private boolean isInit = false;
    private DataAnimatorValue valueAnimator;
    private float param = 0;
    private int length = 0;

    /**
     * 构造
     *
     * @param context 上下文
     */
    public LoadingLine(Context context) {
        super(context);
        init();
    }

    /**
     * 构造
     *
     * @param context 上下文
     * @param attrs 属性集
     */
    public LoadingLine(Context context, AttrSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        addDrawTask(this::onDraw);
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(20);
        backPaint = new Paint();
        backPaint.setColor(Color.RED);
        backPaint.setStrokeWidth(25);
        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(20);
    }

    /**
     * 也可在外部手动控制进度条,自行关闭valueAnimator
     *
     * @param param
     */
    public void setLength(int param) {
        this.length = param;
        invalidate();
    }

    private void start() {
        if (valueAnimator == null) {
            valueAnimator = new DataAnimatorValue();
            valueAnimator.setStart(0);
            valueAnimator.setEnd(100);
            valueAnimator.setCurveType(Animator.CurveType.LINEAR);
            valueAnimator.setDuration(6000);
            valueAnimator.start();
        } else {
            valueAnimator.start();
        }
        eventHandler.postTask(new Runnable() {
            @Override
            public void run() {
                start();
                invalidate();
            }
        }, 10);

        invalidate();
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (!isInit) {
            start();
            isInit = true;
        }
        int width = getWidth();
        int height = getHeight();

        Point startPoint1 = new Point();
        startPoint1.modify(0, (float) (height / 2d));
        Point endPoint1 = new Point();
        endPoint1.modify(width, (float) (height / 2d));
        canvas.drawLine(startPoint1, endPoint1, backPaint);

        Point startPoint2 = new Point();
        startPoint2.modify(0, (float) (height / 2d));
        Point endPoint2 = new Point();
        endPoint2.modify((float) (length * width / 100d), (float) (height / 2d));

        canvas.drawLine(startPoint2, endPoint2, paint);
        canvas.drawText(textPaint, length + "", (float) (length * width / 100d - 22d), (float) (height / 2d + 6d));
        if (valueAnimator.isRunning()) {
            param = (float) valueAnimator.getAnimatedValue();
            setLength((int) param);
        }
    }
}
